package com.yongcheon.sms_manager.data.contract;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.vo.ContactVO;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-26.
 */
public class ContactData {
    public ArrayList<ContactVO> contactVOList;

    public ContactData(ContentResolver contentResolver){
        setContactVOList(contentResolver);
    }

    public void setContactVOList(ContentResolver contentResolver){
        Uri CONTACT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
        String[] selectionArgs = null;
        Cursor contactCursor = contentResolver.query(CONTACT_URI, projection, null, null, null);
        contactVOList = new ArrayList<ContactVO>();

        if (contactCursor.moveToFirst()) {
            do {
                ContactVO acontact = new ContactVO();
                acontact.setContactid(contactCursor.getLong(0));
                acontact.setPhotoid(contactCursor.getLong(1));
                acontact.setPhonenum(contactCursor.getString(2).replaceAll("-", ""));
                acontact.setName(contactCursor.getString(3));
                contactVOList.add(acontact);
            } while (contactCursor.moveToNext());
        }
    }

    public ContactVO getContactByPhoneNum(String ponNum){
        ContactVO contactVO = null;
        for(int i=0; i<contactVOList.size(); i++){
            if(contactVOList.get(i).getPhonenum().equals(ponNum)){
                contactVO = contactVOList.get(i);
                break;
            }
        }
        return contactVO;
    }

    public String getFrom(ContactVO contactvo, String poneNum, Context context){
        String from = "";
        if(contactvo != null){
            from = contactvo.getName() + " ( " + poneNum + " )";
        }else{

            from = context.getResources().getString(R.string.unregistered_number)  + " ( " + poneNum + " )";
        }
        return from;
    }

    public String getFromOnlyName(ContactVO contactvo, Context context){
        String from = "";
        if(contactvo != null){
            from = contactvo.getName();
        }else{

            from = context.getResources().getString(R.string.unregistered_number);
        }
        return from;
    }

    public Drawable getAvatarDrawable(ContactVO contactvo, Context context) {
        Drawable drawable = null;
        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactvo.getContactid());
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);
        if (input != null) {
            Bitmap map =  BitmapFactory.decodeStream(input);
            drawable = new BitmapDrawable(context.getResources(), map);
            try { input.close(); } catch (IOException e) {e.printStackTrace(); }
        }else{
            drawable = context.getResources().getDrawable(R.drawable.ic_contact_picture);
        }
        return drawable;
    }

    public void refresh(ContentResolver contentResolver){
        setContactVOList(contentResolver);
    }
}
